import java.util.concurrent.*;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: ws
 * Date: 2023-12-26
 * Time: 19:34
 */
public class T5 {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        final int a = 10;
        FutureTask<Integer> futureTask = new FutureTask<>(new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                System.out.println("进");
                return 1;
            }
        });

        // 多个线程抢一个 futureTask，仍只执行一次
        Thread thread1 = new Thread(futureTask);
        thread1.start();

        Thread thread2 = new Thread(futureTask);
        thread2.start();

        System.out.println(futureTask.get());

        // 如何执行两次？
        // 两个 futureTask,执行相同的 Callable
        FutureTask<Integer> futureTask2 = new FutureTask<>(new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                System.out.println("jin");
                return 1;
            }
        });


        FutureTask<Integer> futureTask3 = new FutureTask<>(new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                System.out.println("jin");
                return 1;
            }
        });

        new Thread(futureTask2).start();
        new Thread(futureTask3).start();




    }
}
